require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#setUpPwd",
    data:{
        loginUid:userInfo.id,
        infoById:{},
        verificationCode:"",
        password:"",
        password1:""
    },
    mounted:function(){
        this.getUserInfoById();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //通过id获取账户信息
        getUserInfoById:function(){
            var that=this;
            var data={
                loginUid:that.loginUid
                // loginUid:1
            };
            this.ajax({url:"/happyi/getUserInfoById",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.infoById=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取信息失败！");
                }
            });
        },
        //预发短信
        presendMessage:function(){
            var that=this;
            if(!that.password){
                vm.$refs.prompt.toast("请输入密码！");
                return false;
            }
            if(!that.password1){
                vm.$refs.prompt.toast("请再输入密码！");
                return false;
            }
            that.password=lost(that.password);
            that.password1=lost(that.password1);
            if(that.password!=that.password1){
                vm.$refs.prompt.toast("两次密码不一致，请重新输入！");
                that.password="";
                that.password1="";
                return false;
            }


            that.registrationID="cms"+guid();
            var data={
                registrationID:that.registrationID
            };
            console.log(data)
            this.ajax({url:"/user/presendMessage",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.verificationCode=b.json.data.verificationCode;
                    that.setForgetCode();
                }else{
                    vm.$refs.prompt.toast("预发短信失败！");
                }
            });
        },
        //设置忘记密码
        setForgetCode:function(){
            var that=this;
            var data={
                mobile:that.infoById.mobile,
                registrationID:that.registrationID,
                password:that.password
            };
            console.log(data)
            this.ajax({url:"/user/setForgetCode",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.getBack();
                }else{
                    vm.$refs.prompt.toast("操作失败！");
                }
            });
        }
    }
});
//用于生成uuid
function S4() {
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function guid() {
    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
function lost(value){
    var patrn =/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/;
    value=value.replace(/(^\s*)|(\s*$)/g,"");
    if(!(patrn.test(value))){
        vm.$refs.prompt.toast("密码格式不正确！");
    }else{
        return value;
    }
}